package com.example.demo.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class SendChatRot {
    private static Logger logger = LoggerFactory.getLogger(SendChatRot.class);

    //图灵
    public static String tulingMessage(String url, String apiKey, String message, String userId) {
        StringBuffer sb = new StringBuffer();
        sb.append("{" +
                "\"reqType\":0," +
                "    \"perception\": {" +
                "        \"inputText\": {" +
                "            \"text\": \"" + message + "\"" +
                "        }," +
                "        \"inputImage\": {" +
                "            \"url\": \"imageUrl\"" +
                "        }," +
                "        \"selfInfo\": {" +
                "            \"location\": {" +
                "                \"city\": \"河北\"," +
                "                \"province\": \"\"," +
                "                \"street\": \"\"" +
                "            }" +
                "        }" +
                "    }," +
                "    \"userInfo\": {" +
                "        \"apiKey\": \"" + apiKey + "\"," +
                "        \"userId\": \"" + userId + "\"" +
                "    }" +
                "}");

        String result = HttpUtil.sendPost(url, sb.toString());
        logger.info("图灵返回结果：" + result);
        JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
        int code = jsonObject.getJSONObject("intent").getInteger("code");
        Map<String, String> map = new HashMap<>();
        JSONArray results = jsonObject.getJSONArray("results");
        for (int i = 0; i < results.size(); i++) {
            JSONObject j = results.getJSONObject(i);
            String groupType = String.valueOf(j.getInteger("groupType"));
            String text = "";
            if ("url".equals(j.getString("resultType"))) {
                text = j.getJSONObject("values").getString("url");

            } else if ("text".equals(j.getString("resultType"))) {
                text = map.get(String.valueOf(groupType)) + "\n";
            }

            if (map.containsKey(groupType)) {
                String value = map.get(groupType);
                value += "," + text;
                map.put(groupType, value);
            } else {
                map.put(groupType, text);
            }
        }
        StringBuffer sb2 = new StringBuffer();
        map.forEach((m, n) -> {
            String value = n.replaceAll(",", "\n");
            sb2.append(value).append("\n");
        });
        logger.info("图灵获取到的消息：" + sb2.toString());
        return sb2.toString();
    }

    /**
     * 青云客
     * 免费
     */
    public static String qingyunkeMessage(String url, String message) {
        try {
            message = URLEncoder.encode(message, "UTF-8");
            message = message.replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String param = String.format(url, message);
        JSONObject jsonObject = HttpUtil.sendGet(param);
        if ("0".equals(jsonObject.getString("result"))) {
            String result = jsonObject.getString("content");
            result=result.replaceAll("\\{br}","\n");
            return result;
        }
        return "";
    }
}
